﻿@{
    ViewBag.PageTitle = Resources.Intercamber.Login_ResetPassword;
}
@section PageHead
{
    <style>
        .cellTitle
        {
            text-align: right;
        }
        .loginDiv
        {
            margin-top:120px;
            display:inline-block;
            text-align: center;
            padding: 30px;
            width: 600px;
            border: 1px solid gray;
            background-color: white;
        }
    </style>
    <script type="text/javascript">
        var idRequest = "@ViewBag.Id";

        var texts = {
            Error_PasswordNotMatch: "@Resources.Intercamber.Error_PasswordNotMatch",
            Error_PasswordTooSimple: "@Resources.Intercamber.Error_PasswordTooSimple",
            Error_PasswordNeeded: "@Resources.Intercamber.Error_PasswordNeeded"
        };

        function validatePassword(requireFeed) {
            var pwd1 = $("#PasswordInput").val();
            var pwd2 = $("#PasswordConfirm").val();
            if (pwd1 != pwd2)
                return texts.Error_PasswordNotMatch;
            if (pwd1.length > 0 && pwd1.length < 6)
                return texts.Error_PasswordTooSimple;
            if (pwd1.length == 0 && requireFeed)
                return texts.Error_PasswordNeeded;
            $("#Password").val(pwd1 != "" ? Sha256(pwd1) : "");
            return null;
        }

        $(function() {
            $("button").button();
            $("#ResetPwd").click(function() {
                var error = validatePassword(true);
                if (error != null) {
                    $("#errorMessage").html(error);
                    return;
                }
                $("#errorMessage").html("");
                $("#ResetPwd").hide();
                $("#loading").show();
                $.ajax({
                    type: "POST",
                    url: "/Account/ResetPasswordAction",
                    contentType: "application/json; charset=utf-8",
                    data: "{'email':\"" + $("#email").val().replace("\"", "").replace("'", "") + "\", 'id':\"" + idRequest + "\", 'pwd':\"" + Sha256($("#PasswordInput").val()) + "\"}",
                    dataType: "json",
                    success: function(result) {
                        if (result.ok) {
                            $("#errorMessage").html("");
                            $("#infoMessage").html(result.msg);
                            $("#loginDiv").hide();
                            $("#loading").hide();
                            $("#linkConnect").show();
                        } else {
                            $("#errorMessage").html(result.msg);
                            $("#infoMessage").html("");
                            $("#email").focus();
                            $("#ResetPwd").show();
                            $("#loading").hide();
                        }
                    },
                    error: function(result) {
                        $("#ResetPwd").show();
                        debugger;
                    }
                });
            });

        });

    </script>
}

@section PageContent {
    <div class="innerTitle ui-corner-all">
        @Resources.Intercamber.Login_ResetPassword
    </div>
    <br />
    <br />

    <div align="center" style="width:100%;">
        <div class="ui-corner-all loginDiv">
            @Resources.Intercamber.ResetPassword_HeadMessage
            <br/>
            <br/>
            <label id="errorMessage" style="color:red"></label>
            <label id="infoMessage" style="color:forestgreen"></label>
            <label id="linkConnect" style="display: none">
                <br/>
                <a href="~/Account/Login">@Resources.Intercamber.Connection</a>
            </label>
            <div id="dataDiv" style="margin-left:200px;">
                <table>
                    <tr>
                        <td class="cellTitle">@Resources.Intercamber.Email</td>
                        <td>
                            <input type="text" id="email" value="" style="width: 200px;" />
                        </td>
                    <tr/>
                    <tr>
                        <td class="cellTitle">@Resources.Intercamber.NewPassword</td>
                        <td>
                            <input type="password" id="PasswordInput" value="" style="width: 200px;" />
                        </td>
                    <tr/>
                    <tr>
                        <td class="cellTitle">@Resources.Intercamber.NewPasswordConfirm</td>
                        <td>
                            <input type="password" id="PasswordConfirm" value="" style="width: 200px;" />
                        </td>
                    </tr>
                </table>
            </div>
            <br/>
            <br/>
            <button id="ResetPwd" onclick=""  class="" >@Resources.Intercamber.ResetPassword_ChangePassword</button>
            <img src="~/Images/Loading.gif" id="loading" style="display:none" />
        </div>
    </div>
}
